package de.stefanocke.roo.wicket.form

import de.stefanocke.roo.wicket.Stereotypes
import de.stefanocke.roo.wicket.base.WicketItdBuilder
import java.lang.reflect.Modifier

import static de.stefanocke.roo.xtend.Method.*
import static de.stefanocke.roo.xtend.MethodSignature.*
import static de.stefanocke.roo.xtend.Parameter.*
import static de.stefanocke.roo.xtend.RooExtensions.*

class FormItdBuilder extends WicketItdBuilder{
	
	override FormMetadata getMetadata(){super.metadata as FormMetadata} 
	def formFieldsType(){metadata.formFieldsMetadata.destination}
	
	override String baseClassStereotype(){
		Stereotypes::FORM	
	}
	
	override getMethods()
	{
			var methods = constructors
			methods += getCreateFormFieldsMethod
			methods
	}
	
	def getConstructors(){
		newArrayList(
			constructorWithIdAndModelAndServiceBindings
		)
	}
	
	//	public KontaktForm(String id, IModel<Kontakt> model,
	//			IServiceBindings<Kontakt, ?> serviceBindings) {
	//		super(id, model, serviceBindings);
	//	}
	def constructorWithIdAndModelAndServiceBindings(){
		method(
			signature(Modifier::PUBLIC, 
				param("java.lang.String", "id"), 
				modelParam(modelClass, "model"),
				param(Stereotypes::IENTITYBINDINGS.stereotype(modelClass, WILDCARD), "serviceBindings")
			),
			[|'''
			super(id, model, serviceBindings);
			''']
		)
	}
	
	//	protected Panel createFormFields(IModel<Foo> model) {
	//		return new FooFormFields(model);
	//	}
	def getCreateFormFieldsMethod(){
		val returnType = Stereotypes::FORM_FIELDS.stereotype(modelClass)
		method(
			signature(
				Modifier::PUBLIC, returnType, "createFormFields", modelParam(modelClass, "model")
			),
			[|'''
			return new «formFieldsType.resolve»(model);
			''']
			
		)
	}
	
}